import express, { NextFunction, Request, Response } from "express";
import { router } from "./routes";
import cookieParser from "cookie-parser";
import cors from "cors";
import { config } from "dotenv";

config();

const app = express();

app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(
  cors({
    origin: process.env.CORS,
  }),
);
app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
  console.error(err.message, err);
  return res.status(400).json({
    error: err.message,
  });
});

app.use("/api", router);

export { app };
